﻿using UnityEngine;
using System.Collections;

public class CricleTurn : MonoBehaviour 
{
  public Transform red;
  public Transform blue;
  public Vector2 cricleCenter;
  public float radius;
  private float angle = 0.0f;

  void Start()
  {
    angle = Mathf.PI * 0.5f;
    Vector2 pos = calcCriclePosition(cricleCenter, angle, radius);
    red.localPosition = new Vector3(pos.x, pos.y, 0);
    Vector2 pos1 = new Vector2(cricleCenter.x - (pos.x - cricleCenter.x), cricleCenter.y - (pos.y - cricleCenter.y));
    blue.localPosition = new Vector3(pos1.x, pos1.y, 0);
  }


  public void Update()
  {
    if (!GlobalSetting.Turn)
      return;

    if (GlobalSetting.Direction == GlobalSetting.TurnDirection.Left)
      angle = (angle + Time.deltaTime * Mathf.PI) % (Mathf.PI * 2);
    else
      angle = (angle - Time.deltaTime * Mathf.PI) % (Mathf.PI * 2);

    Vector2 pos = calcCriclePosition(cricleCenter, angle, radius);
    red.localPosition = new Vector3(pos.x, pos.y, 0);
    Vector2 pos1 = new Vector2(cricleCenter.x - (pos.x - cricleCenter.x), cricleCenter.y - (pos.y - cricleCenter.y));
    blue.localPosition = new Vector3(pos1.x, pos1.y, 0);
  }

  public Vector2 calcCriclePosition(Vector2 cricleCenter, float angle, float radius)
  {
    float x = radius * Mathf.Sin(angle);
    float y = radius * Mathf.Cos(angle);

    return new Vector2(cricleCenter.x + x, cricleCenter.y + y);
  }
}
